package com.my.vote.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.my.vote.R;
import com.my.vote.adapter.ItemAdapter;
import com.my.vote.entity.EVoteInfo;
import com.my.vote.entity.EVoteItem;
import com.my.vote.entity.req.ReqVoteItem;
import com.my.vote.pst.info.IPstVoteInfo;
import com.my.vote.widget.RxTimeButton;
import com.my.vote.widget.SwipeRefreshMoreLayout;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.annotation.RxBusEvent;
import com.sfh.lib.mvp.service.LoaderService;
import com.sfh.lib.ui.AbstractActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Action;

/**
 * 功能描述:详情
 *
 * @author
 * @date 2018/4/11
 */
public class VoteInfoActivity extends AbstractActivity implements SwipeRefreshMoreLayout.OnLoadingListener, IPstVoteInfo.IVItem {
    @BindView(R.id.ivURL)
    ImageView ivURL;
    @BindView(R.id.tvStatus)
    TextView tvStatus;
    @BindView(R.id.tvTime)
    RxTimeButton tvTime;
    @BindView(R.id.tvDesc)
    TextView tvDesc;
    @BindView(R.id.tvLeft)
    TextView tvLeft;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.tvCandidateSize)
    TextView tvCandidateSize;
    @BindView(R.id.tvVotingPersonSize)
    TextView tvVotingPersonSize;
    @BindView(R.id.tvVisitSize)
    TextView tvVisitSize;
    @BindView(R.id.base_refresh)
    SwipeRefreshMoreLayout baseRefresh;
    @BindView(R.id.btVoteOK)
    ImageView btVoteOK;

    public static void startUI(Context context, String voteId) {
        Intent intent = new Intent(context, VoteInfoActivity.class);
        intent.putExtra("DATA", voteId);
        context.startActivity(intent);
    }


    EVoteInfo info;
    ItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vote_info);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        String voteId = getIntent().getStringExtra("DATA");
        if (TextUtils.isEmpty(voteId)) {
            return;
        }
        pstItem.getVote(voteId);

        adapter = new ItemAdapter(this);
        baseRefresh.setOnLoadingListener(this);
        baseRefresh.addItemDecorationSize(5);
        baseRefresh.setAdapter(adapter, true);

    }

    Action onComplete = new Action() {
        @Override
        public void run() throws Exception {
            //倒计时结束刷新列表
        }
    };

    @OnClick({R.id.tvLeft, R.id.tvRight,R.id.btVoteOK})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvLeft:

                finish();
                break;
            case R.id.tvRight:
                // 报名
                SignUpVoteActivity.startUI(this, info.getObjectId());
                break;
            case R.id.btVoteOK:
                // 投票
                pstItem.voteItem(info,adapter.getSelect());
                break;
        }
    }

    IPstVoteInfo pstItem;

    @Override
    public IPresenter getPresenter() {
        if (pstItem == null) {
            pstItem = LoaderService.getService(IPstVoteInfo.class);
        }
        return pstItem;
    }

    int nowPage = 0;

    @Override
    public void onPageList(int nowPage, int allSize, List<EVoteItem> page) {
        this.nowPage = nowPage;
        this.baseRefresh.onRefreshCompale(page, nowPage != 0);
        if (allSize == nowPage) {
            this.baseRefresh.onLoadAllCompale();
        }
    }


    @Override
    public void onPageError(String error) {
        this.baseRefresh.onLoadMoreFail();
        this.showToast(error);
    }

    @Override
    public void onVoteInfo(EVoteInfo voteInfo) {
        this.info = voteInfo;
        tvLeft.setText(info.getTitle());
        tvDesc.setText(info.getIntroduce());
        String statue = info.getStatue();


        if (TextUtils.equals("2", statue) && TextUtils.equals("1", info.getType())) {
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText("SignUp");
        }
        tvStatus.setText(info.getStatueText(statue));
        tvStatus.setBackgroundResource(info.getStatueDrawable(statue));

        if (TextUtils.equals("1", statue)) {
            btVoteOK.setVisibility(View.VISIBLE);
            tvTime.startInterval((long) ((info.getVoteEndTime() - System.currentTimeMillis()) / 1000), onComplete);
        } else if (TextUtils.equals("2", statue)) {
            btVoteOK.setVisibility(View.GONE);
            tvTime.startInterval((long) ((info.getEnrollEndTime() - System.currentTimeMillis()) / 1000), onComplete);
        } else {
            tvTime.stopInterval();
            btVoteOK.setVisibility(View.GONE);
            tvTime.setText("Vote End");
        }

        tvCandidateSize.setText(voteInfo.getCandidateSize() != null ? voteInfo.getCandidateSize()+"" : "0");
        tvVotingPersonSize.setText(voteInfo.getVotingPersonSize() != null ? voteInfo.getVotingPersonSize()+"" : "0");
        tvVisitSize.setText(voteInfo.getVisitSize() != null ? voteInfo.getVisitSize()+"" : "0");
        Picasso.with(this).load(info.getVoteUrl()).into(ivURL);
        baseRefresh.onAutoRefresh();
    }

    @Override
    public void voteSuccess() {
        adapter.clearSelect();
        pstItem.getVote(info.getObjectId());
    }

    @Override
    public void onRefreshUI() {
        this.nowPage = 0;
        ReqVoteItem req = new ReqVoteItem();
        req.setNowPage(nowPage);
        req.setVoteId(info.getObjectId());
        pstItem.listPage(req);
    }

    @Override
    public void onLoadMoreUI() {
        this.nowPage += 1;
        ReqVoteItem req = new ReqVoteItem();
        req.setNowPage(nowPage);
        req.setVoteId(info.getObjectId());
        pstItem.listPage(req);
    }

    @RxBusEvent(taskId = 1000,eventClass = SignUpVoteActivity.SingUpToast.class)
    public void rxEvent( SignUpVoteActivity.SingUpToast toast){
        pstItem.getVote(info.getObjectId());
    }

}
